// subpkg/medicine/medicine.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value1:0,
    option1: [
      
      { text: '这周用药情况', value: 0 },
      { text: '上周用药情况', value: 1 }
    ],
    i:0,
    currentDate:'2023/8/1',
    medicineList:
    [
    ],
    medicineShow_thisWeek:[],
    medicineShow_lastWeek:[],
    medicineShow:[]
  },
  onChange(e){
    this.setData({
      value1:e.detail
    })
    if(this.data.value1 == 0){
      this.setData({
        medicineShow:this.data.medicineShow_thisWeek
      })
      if(this.data.medicineShow_thisWeek.length==0){
        Toast('无记录')
      }
      
    }else{
      this.setData({
        medicineShow:this.data.medicineShow_lastWeek
      })
      if(this.data.medicineShow_lastWeek.length==0){
        Toast('无记录')
      }

      
    }

    
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.query()
  },
  query(){
    const that = this
    wx.request({
      url: 'https://zhixiao.site:8080/client/medication_record/selectAll',
      data:{
        elderlyId:wx.getStorageSync('elderId')
      },
      success:(res)=>{
        var medicineList = []
        var medicineList1 = res.data.data
        console.log(medicineList1)
        for (var i in medicineList1){
          console.log(medicineList1[i])
          var name = medicineList1[i].medicationList.medication.name
          var time = medicineList1[i].time
          var status = medicineList1[i].status
          var date = medicineList1[i].createTime.slice(0,10)
          medicineList.push({name:name,time:time,status:status,date:date})
        }
        this.setData({
          medicineList:medicineList
        })
      },
      complete:(res)=>{
        var date = new Date()
        var millSecond = date.getTime()
        //展示这周的
        var this_millSecond = millSecond - 7*24*60*60*1000
        var date1 = new Date(this_millSecond)
        var year = date1.getFullYear()
        var month = date1.getMonth() + 1 
        month= month < 10?'0'+month:month
        
        var day = date1.getDate()
        day = day < 10?'0'+day:day

        var this_date = `${year}-${month}-${day}`
        
        var this_millSecond1 = millSecond - 14*24*60*60*1000
        var date2 = new Date(this_millSecond1)
        var year1 = date2.getFullYear()
        var month1 = date2.getMonth() + 1 
        month1 = month1 < 10?'0'+month1:month1
        var day1 = date2.getDate()
        day1 = day1 < 10?'0'+day1:day1
        var this_date1 = `${year1}-${month1}-${day1}`
        console.log(this_date1)
        console.log(this_date)
        var medicineShow_thisWeek = []
        var medicineShow_lastWeek = []
        for(var i=0;i<this.data.medicineList.length;i++){
         
          if(this.data.medicineList[i].date<this_date1){
            break;
          }
          else if(this.data.medicineList[i].date<this_date){
            medicineShow_lastWeek.push(this.data.medicineList[i])
          }
          else{ 
            medicineShow_thisWeek.push(this.data.medicineList[i])
          }

        }
        this.setData({
          medicineShow_lastWeek:medicineShow_lastWeek,
          medicineShow_thisWeek:medicineShow_thisWeek,
          medicineShow:medicineShow_thisWeek
        })
        if(this.data.medicineShow_thisWeek.length==0){
          Toast('无记录')
        }

      }
        
    })
    
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})